home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STRINGS.SWG / 0020_String Centering.pas < prev    next >
Pascal/Delphi Source File  |  1993-07-16  |  3KB  |  96 lines

  1. (*
  2. ===========================================================================
  3.  BBS: Canada Remote Systems
  4. Date: 06-25-93 (13:52)             Number: 25767
  5. From: GUY MCLOUGHLIN               Refer#: NONE
  6.   To: CHRIS PRIEDE                  Recvd: NO
  7. Subj: STRING CENTERING ROUTINES      Conf: (552) R-TP
  8. ---------------------------------------------------------------------------
  9.  
  10.   Hi, Chris:
  11.  
  12. CP>Ideally such function should be written in assembly, but since this
  13. CP>is Pascal conference and I've flooded it with my assembly code enough
  14. CP>lately, we will use plain Turbo Pascal.
  15.  
  16.   Try running this program using your routine and the one I posted,
  17.   you might notice something "funny" about the ouput displayed. <g>
  18. *)
  19.  
  20. {$A+,B-,D-,E-,F-,G-,I-,L-,N-,O-,P-,Q-,R-,S-,T-,V-,X+}
  21. {$M 1024,0,0}
  22.  
  23. program DemoStringRoutines;
  24.  
  25. USES Crt;
  26.  
  27.   function FCenter(S: string; W: byte): string;
  28.   var
  29.     SpaceCnt: byte;
  30.   begin
  31.     if Length(S) < W then
  32.       begin
  33.         SpaceCnt := (W - Length(S)) div 2;
  34.         Move(S[1], S[1+SpaceCnt], Length(S));
  35.         FillChar(S[1], SpaceCnt, '-');
  36.         S[0] := Chr(Length(S) + SpaceCnt);
  37.       end;
  38.     FCenter := S;
  39.   end;
  40.  
  41.               (* Set these constants according to your needs.         *)
  42.   const
  43.     BlankChar   = '-';
  44.     ScreenWidth = 80;
  45.  
  46.     (***** Create video-display string with input string centered.    *)
  47.     (*                                                                *)
  48.     function CenterVidStr({input} InText : string) : {output} string;
  49.     var
  50.       InsertPos : byte;
  51.       TempStr   : string;
  52.     begin
  53.               (* Initialize TempStr.                                  *)
  54.       TempStr[0] := chr(ScreenWidth);
  55.       fillchar(TempStr[1], ScreenWidth, BlankChar);
  56.  
  57.               (* Calculate string insertion position.                 *)
  58.       InsertPos := succ((ScreenWidth - length(InText)) div 2);
  59.  
  60.               (* Insert text in the center of TempStr.                *)
  61.       move(InText[1], TempStr[InsertPos], length(InText));
  62.  
  63.               (* Return function result.                              *)
  64.       CenterVidStr := TempStr
  65.  
  66.     end;      (* CenterVidStr.                                        *)
  67.  
  68. var
  69.   TempStr : string;
  70.  
  71. BEGIN
  72.   Clrscr;
  73.   fillchar(TempStr[1], 30, 'X');
  74.   TempStr[0] := #30;
  75.   writeln(FCenter(TempStr, 80));
  76.   writeln(CenterVidStr(TempStr))
  77. END.
  78.  
  79.   ...I tried timing these two routines on my PC (Recently upgraded
  80.   to a 386dx-40 AMD motherboard), and here are the results:
  81.  
  82.  Compiler │ Length │ Your routine │ My routine │ Ratio
  83. ──────────┼────────┼──────────────┼────────────┼────────
  84.  TP 7     │   30   │    0.03167   │   0.04043  │ 1.28
  85. ──────────┼────────┼──────────────┼────────────┼────────
  86.  PASCAL+  │   30   │    0.02037   │   0.01959  │ 0.96
  87.  
  88.  *** Both functions were called in a loop 1000 times on each run,
  89.      result was discarded ($X+ directive).
  90.  
  91.  For curiosity sake I'll post the StonyBrook PASCAL+ machine-code
  92.  listing in the next message.
  93.                                - Guy
  94. ---
  95.  ■ DeLuxe²/386 1.25 #5060 ■
  96.